package com.example.consumer.service;

import com.example.common.Constant;
import com.example.common.MyUtils;
import com.example.common.RandomUtils;
import com.example.common.SolutionCount;
import com.example.consumer.Board2;
import com.example.consumer.SpringConfig;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyRabbitListener {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 对死信队列的消息处理后，重新向工作队列投递
     * @param boardCharArray
     */
    @RabbitListener(queues = {SpringConfig.DEAD_QUEUE})
    public void listen(char[][] boardCharArray) {
        boolean flag = false;
        Board2 board2 = null;
        while (!flag) {
            board2 = new Board2(MyUtils.copyOf(boardCharArray));
            board2.randomInsert(3);
            SolutionCount solutionCount = new SolutionCount();
            int n = solutionCount.solve(board2.getBoard());
            if (n > 0 && n <= Constant.LIMIT) {
                flag = true;
            }
        }
        System.out.println(board2.toString());
        rabbitTemplate.convertAndSend("work.queue", board2.getBoard());
    }
}
